//
//  CorgiEmoticonTools.swift
//  weibo
//
//  Created by Corgi on 17/3/30.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit

let CORGIEMOTICONCOL = 7
let CORGIEMOTICONROW = 3
let CORGIEMOTICONCOUNT = CORGIEMOTICONCOL * CORGIEMOTICONROW - 1

class CorgiEmoticonTools: NSObject {

    static let shared: CorgiEmoticonTools = CorgiEmoticonTools()
    
    fileprivate let path = ((NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!) as NSString).appendingPathComponent("emoticon.archiver")
    
    lazy var emoticonsBundle: Bundle = {
    
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        
        return Bundle(path: path)!
    }()
    
    lazy var recentEmoticons: [CorgiEmoticonModel] = {
    
        return self.getRecentModel()
    }()
    
    lazy var defaultEmoticons: [CorgiEmoticonModel] = {
    
        return self.getEmoticonsWithPlist(path: "default")
    }()
    
    lazy var emojiEmoticons: [CorgiEmoticonModel] = {
        
        return self.getEmoticonsWithPlist(path: "emoji")
    }()
    
    lazy var lxhEmoticons: [CorgiEmoticonModel] = {
        
        return self.getEmoticonsWithPlist(path: "lxh")
    }()
    
    lazy var allEmoticons:[[[CorgiEmoticonModel]]] = {
    
        return [[self.recentEmoticons],
                self.getEmoticonsGroup(emoticons: self.defaultEmoticons),
                self.getEmoticonsGroup(emoticons: self.emojiEmoticons),
                self.getEmoticonsGroup(emoticons: self.lxhEmoticons)]
    }()
    
    func getEmoticonsGroup(emoticons: [CorgiEmoticonModel]) -> [[CorgiEmoticonModel]] {
        
        let pageCount = (emoticons.count + CORGIEMOTICONCOUNT - 1)/CORGIEMOTICONCOUNT
        
        var tempArray: [[CorgiEmoticonModel]] = [[CorgiEmoticonModel]]()
        
        for i in 0..<pageCount {
        
            let location = CORGIEMOTICONCOUNT * i
        
            var length = CORGIEMOTICONCOUNT
            
            if location + length > emoticons.count {
            
                length = emoticons.count - location
            }
        
            let range = NSRange(location: location, length: length)
            
            let array = (emoticons as NSArray).subarray(with: range) as! [CorgiEmoticonModel]
            
            tempArray.append(array)
        }
        
        return tempArray
    }
    
    
}

extension CorgiEmoticonTools {

    fileprivate func getEmoticonsWithPlist(path: String) -> [CorgiEmoticonModel]{
        
        let file = emoticonsBundle.path(forResource: "\(path)/info.plist", ofType: nil)!
        
        let plistArray = NSArray(contentsOfFile: file)!
        
        let tempArray = NSArray.yy_modelArray(with: CorgiEmoticonModel.self, json: plistArray) as! [CorgiEmoticonModel]
        
        for emoticonModel in tempArray {
        
            let png = emoticonModel.png ?? ""
            
            emoticonModel.allPath = path + "/" + png
        }
        
        return tempArray
    }

}

extension CorgiEmoticonTools {

    func saveRecentModel(model: CorgiEmoticonModel) {
        
        for (i, emoticonModel) in recentEmoticons.enumerated() {
        
            if emoticonModel.isEmoji {
            
                if emoticonModel.code == model.code {
                
                    recentEmoticons.remove(at: i)
                }
            } else {
            
                if emoticonModel.png == model.png {
                    
                    recentEmoticons.remove(at: i)
                }
            }
        }
        
        recentEmoticons.insert(model, at: 0)
        
        if recentEmoticons.count > 20 {
        
            recentEmoticons.removeLast()
        }
        
        allEmoticons[0][0] = recentEmoticons
        
        NSKeyedArchiver.archiveRootObject(recentEmoticons, toFile: path)
    }
    
    fileprivate func getRecentModel() -> [CorgiEmoticonModel] {
        
        if let result = NSKeyedUnarchiver.unarchiveObject(withFile: path) as? [CorgiEmoticonModel] {
        
            return result
        }
        
        return [CorgiEmoticonModel]()
    }
}

extension CorgiEmoticonTools {
    
    func getEmoticonModelWithEmoticons(chs: String) -> CorgiEmoticonModel? {
        
        for model in defaultEmoticons {
        
            if model.chs == chs {
            
                return model
            }
        }
        
        for model in lxhEmoticons {
            
            if model.chs == chs {
                
                return model
            }
        }
        
        return nil
    }
}

